<!--
  * 部门表单 弹窗
-->
<template>
  <a-modal v-model:visible="visible" :title="formState.id ? '编辑部门' : '添加部门'" @ok="handleOk" destroyOnClose>
    <a-form ref="formRef" :model="formState" :rules="rules" layout="vertical">
      <a-form-item label="上级部门" name="parentId" v-if="formState.parentId != 0">
        <DepartmentTreeSelect ref="departmentTreeSelect" v-model:value="formState.parentId" :defaultValueFlag="false" width="100%" />
      </a-form-item>
      <a-form-item label="部门名称" name="name">
        <a-input v-model:value.trim="formState.name" placeholder="请输入部门名称" />
      </a-form-item>
      <a-form-item label="部门简称" name="shortName">
            <a-input v-model:value.trim="formState.shortName" placeholder="请输入部门简称" />
      </a-form-item>
      <a-form-item label="部门地址" name="addr">
            <a-input v-model:value.trim="formState.addr" placeholder="请输入部门地址" />
      </a-form-item>
      <a-form-item label="部门排序 （值越大越靠前！）" name="showOrder">
        <a-input-number style="width: 100%" v-model:value="formState.showOrder" :min="0" placeholder="请输入部门名称" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup lang="ts">
  import message from 'ant-design-vue/lib/message';
  import { reactive, ref } from 'vue';
  import { departmentApi } from '/@/api/system/department/department-api';
  import DepartmentTreeSelect from '/@/components/system/department-tree-select/index.vue';
  import { smartSentry } from '/@/lib/smart-sentry';
  import { SmartLoading } from '/@/components/framework/smart-loading';

  // ----------------------- 对外暴漏 ---------------------

  defineExpose({
    showModal,
  });

  // ----------------------- modal 的显示与隐藏 ---------------------
  const emits = defineEmits(['refresh']);

  const visible = ref(false);
  function showModal(data) {
    visible.value = true;
    updateFormData(data);
  }
  function closeModal() {
    visible.value = false;
    resetFormData();
  }

  // ----------------------- form 表单操作 ---------------------
  const formRef = ref();
  const departmentTreeSelect = ref();
  const defaultDepartmentForm = {
    id: undefined,
    shortName: undefined, 
    name: undefined,
    addr:undefined,
    parentId: undefined,
    showOrder: 0,
  };

  let formState = reactive({
    ...defaultDepartmentForm,
  });
  // 表单校验规则
  const rules = {
    parentId: [{ required: true, message: '上级部门不能为空' }],
    name: [
      { required: true, message: '部门名称不能为空' },
      { max: 50, message: '部门名称不能大于20个字符', trigger: 'blur' },
    ],
    shortName: [{ required: true, message: '部门简称不能为空' }],
  };
  // 更新表单数据
  function updateFormData(data) {
    Object.assign(formState, defaultDepartmentForm);
    if (data) {
      Object.assign(formState, data);
    }
    visible.value = true;
  }
  // 重置表单数据
  function resetFormData() {
    Object.assign(formState, defaultDepartmentForm);
  }

  async function handleOk() {
    try {
      await formRef.value.validate();
      saveDepartment();
    } catch (error) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // ----------------------- form 表单  ajax 操作 ---------------------

  //保存部门ajax请求
  async function saveDepartment() {
    SmartLoading.show();
    try {
      if (formState.parentId == formState.id) {
        message.warning('上级菜单不能为自己');
        return;
      }
      let res = await departmentApi.saveDepartment(formState);
      if(res.success){
        emits('refresh');
        closeModal();
      }else
        message.error(res.message);
    } catch (error) {
      smartSentry.captureError(error);
    } finally {
      SmartLoading.hide();
    }
  }
</script>
